package distrcomp;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

import javax.rmi.ssl.SslRMIClientSocketFactory;
import javax.rmi.ssl.SslRMIServerSocketFactory;

public class RegisterSubserverImpl extends UnicastRemoteObject implements RegisterSubserver {
	private static final long serialVersionUID = 1L;
	private Server server;

	protected RegisterSubserverImpl(Server server) throws RemoteException {
		super(0, new SslRMIClientSocketFactory(), new SslRMIServerSocketFactory(null, null, true));
		this.server = server;
	}

	public int register(Subserver subserver) throws RemoteException {
		int ID = this.server.addSubserver(subserver);
		subserver.setPrices(server.getRate(ShippingType.GROUND), server.getRate(ShippingType.AIR), server.getRate(ShippingType.AIR_PRIORITY));
		return ID;
	}

	//Allows a server which restores itself after a crash to bind with its original ID
	public void register(Subserver subserver, int ID) throws Exception {
		this.server.addSubserver(subserver, ID);
	}
}
